<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增工序资料"
    width="900px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="工序编码" required>
            <el-input v-model="state.form.opCode" placeholder="请填写编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序名称" required>
            <el-input v-model="state.form.opName" placeholder="请填写编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心">
            <el-select
              v-model="state.form.pkWorkcenter"
              placeholder="中心"
              style="width: 100%"
            >
              <el-option
                v-for="wc in wcs"
                :key="wc.pkWc"
                :label="wc.wcName"
                :value="wc.pkWc"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备料仓库">
            <el-select
              v-model="state.form.blWhPk"
              placeholder="备料仓库"
              style="width: 100%"
            >
              <el-option
                v-for="wh in whs"
                :key="wh.pkWh"
                :label="wh.whName"
                :value="wh.pkWh"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="默认仓库">
            <el-select
              v-model="state.form.defaultRkWhPk"
              placeholder="默认入库仓库"
              style="width: 100%"
            >
              <el-option
                v-for="wh in whs"
                :key="wh.pkWh"
                :label="wh.whName"
                :value="wh.pkWh"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-checkbox v-model="state.form.isKpi" label="计算绩效" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { getWarehouseListApi } from '@/api/base/wh'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { getWcListApi } from '@/api/base/wc'
  import { addOperApi } from '@/api/base/op'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const dialogVisible = ref(false)

  const state = reactive({
    form: {
      pkCorp: pkCorp,
    },
  })

  const wcs = ref([])
  const whs = ref([])

  const openDialog = () => {
    getWarehouseList()
    getWcList()
    dialogVisible.value = true
  }

  const getWarehouseList = async () => {
    let con = { pkCorp: pkCorp }
    const res = await getWarehouseListApi(con)
    if (res.code === 2000) {
      whs.value = res.data
    }
  }

  const getWcList = async () => {
    let con = { pkCorp: pkCorp }
    const res = await getWcListApi(con)
    if (res.code === 2000) {
      wcs.value = res.data
    }
  }

  const save = async () => {
    const load = $baseLoading()
    try {
      const res = await addOperApi(state.form)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
